package com.javasm.mingming.adminuser.service.impl;

import com.javasm.mingming.adminuser.entity.AdminUser;
import com.javasm.mingming.adminuser.service.AdminSecurityService;
import com.javasm.mingming.adminuser.service.AdminUserService;
import com.javasm.mingming.common.exception.JavasmException;
import com.javasm.mingming.common.exception.JavasmExceptionEnum;
import com.javasm.mingming.login.entity.LoginUserDetails;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Administrator
 * @date 2025-01-22 21:52
 */
@Service
public class AdminSecurityServiceImpl implements AdminSecurityService {
    @Resource
    PasswordEncoder passwordEncoder;
    @Resource
    AdminUserService adminUserService;

    @Override
    public AdminUser queryMyUser() {
        //从Security中获取 用户信息
        //获取上下文对象
        SecurityContext context = SecurityContextHolder.getContext();
        //包含用户信息 UsernamePasswordAuthenticationToken
        Authentication authentication = context.getAuthentication();
        if (authentication == null){
            throw new JavasmException(JavasmExceptionEnum.NotLogin);
        }
        //已经登录  UserDetails
        Object principal = authentication.getPrincipal();
        LoginUserDetails loginUserDetails = (LoginUserDetails) principal;
        AdminUser adminUser = loginUserDetails.getAdminUser();
        return adminUser;
    }

    @Override
    public void updateAdminUser(AdminUser adminUser) {
        //获取,现在已经登录的用户信息
        AdminUser loginUser = queryMyUser();//Security中获取
        //修改数据库的信息
        //可能被修改的值
        if (adminUser.getPassword() != null){
            //修改密码
            String s = passwordEncoder.encode(adminUser.getPassword());
            loginUser.setPassword(s);
        }
        if (adminUser.getRoleId() != null){
            loginUser.setRoleId(adminUser.getRoleId());
        }
        if (adminUser.getIsvalid() != null){
            loginUser.setIsvalid(adminUser.getIsvalid());
        }
        if (adminUser.getRemark() != null){
            loginUser.setRemark(adminUser.getRemark());
        }
        if (adminUser.getHeadImg() != null){
            loginUser.setHeadImg(adminUser.getHeadImg());
        }
     adminUserService.updateById(loginUser);
        //数据库更新结束
        //修改security中的用户信息
        //模仿源码中,把Authentication重新new出来
        LoginUserDetails loginUserDetails = new LoginUserDetails(loginUser);
        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(
                        loginUserDetails,
                        loginUserDetails.getPassword(),
                        loginUserDetails.getAuthorities()
                );
        //重新放回上下文对象
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
    }


}
